package com.galgram.project.controller;

import java.util.List;
import java.util.ArrayList;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.galgram.common.annotation.Log;
import com.galgram.common.core.controller.BaseController;
import com.galgram.common.core.domain.AjaxResult;
import com.galgram.common.enums.BusinessType;
import com.galgram.project.domain.FxActivity;
import com.galgram.project.service.IFxActivityService;
import com.galgram.common.utils.poi.ExcelUtil;
import com.galgram.common.core.page.TableDataInfo;

/**
 * 活动Controller
 *
 * @author galgram
 * @date 2024-11-20
 */
@RestController
@RequestMapping("/project/activity")
public class FxActivityController extends BaseController {
    @Autowired
    private IFxActivityService fxActivityService;

    /**
     * 查询活动列表
     */
    @GetMapping("/getList")
    public TableDataInfo getList(FxActivity fxActivity) {
        startPage();
        List<FxActivity> list = fxActivityService.getActivityList(fxActivity);
        return getDataTable(list);
    }

    /**
     * 查询活动列表
     */
    @PreAuthorize("@ss.hasPermi('project:activity:list')")
    @GetMapping("/list")
    public TableDataInfo list(FxActivity fxActivity) {
        startPage();
        List<FxActivity> list = fxActivityService.selectFxActivityList(fxActivity);
        return getDataTable(list);
    }

    /**
     * 获取活动详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:activity:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(fxActivityService.getById(id));
    }

    /**
     * 新增活动
     */
    @PreAuthorize("@ss.hasPermi('project:activity:add')")
    @Log(title = "活动", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FxActivity fxActivity) {
        fxActivityService.save(fxActivity);
        return success("添加成功");
    }

    /**
     * 修改活动
     */
    @PreAuthorize("@ss.hasPermi('project:activity:edit')")
    @Log(title = "活动", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FxActivity fxActivity) {
        fxActivityService.updateById(fxActivity);
        return success("修改成功");
    }

    /**
     * 删除活动
     */
    @PreAuthorize("@ss.hasPermi('project:activity:remove')")
    @Log(title = "活动", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        fxActivityService.removeByIds(ids);
        return success("删除成功");
    }


    /**
     * 导出活动列表
     */
    @PreAuthorize("@ss.hasPermi('project:activity:export')")
    @Log(title = "活动", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FxActivity fxActivity) {
        List<FxActivity> list = fxActivityService.selectFxActivityList(fxActivity);
        ExcelUtil<FxActivity> util = new ExcelUtil<FxActivity>(FxActivity.class);
        util.exportExcel(response, list, "活动数据");
    }

    /**
     * 导入活动列表
     */
    @PreAuthorize("@ss.hasPermi('project:activity:import')")
    @Log(title = "活动", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<FxActivity> util = new ExcelUtil<FxActivity>(FxActivity.class);
        List<FxActivity> userList = util.importExcel(file.getInputStream());
        fxActivityService.saveBatch(userList);
        return AjaxResult.success("导入成功");
    }

    /**
     * 下载模版
     */
    @PreAuthorize("@ss.hasPermi('project:activity:export')")
    @PostMapping("/importTemplate")
    public void exportTemplate(HttpServletResponse response) {
        List<FxActivity> list = new ArrayList<FxActivity>();
        ExcelUtil<FxActivity> util = new ExcelUtil<FxActivity>(FxActivity.class);
        util.exportExcel(response, list, "活动数据");
    }
}
